package com.example.Activity.controller;

import com.example.Activity.domain.Activity;
import com.example.Activity.domain.Bo.ActivityAddBo;
import com.example.Activity.domain.Vo.ActivityListVo;
import com.example.Activity.service.IActivityService;
import com.example.comon.domain.LoginUser;
import com.example.comon.domain.R;
import com.example.comon.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/activity")
@Api(value="activity",tags="活动管理")
public class ActivityController {
    @Autowired
    private IActivityService activityService;

    @GetMapping("/list")
    @ApiOperation("获取活动列表")
    public R<List<ActivityListVo>> getActivityList(ActivityAddBo bo) {
        Activity activity = new Activity();
        activity.setId(bo.getId());
        activity.setActivity(bo.getActivity());
        activity.setActivityType(bo.getActivityType());
        activity.setState(bo.getState());
        List<ActivityListVo> list = activityService.selectActivity(activity);
        return R.success(list);
    }

    @PostMapping("/add")
    @ApiOperation("新增活动")
    public R<Activity> addActivity(@RequestBody ActivityAddBo bo) {
        LoginUser loginUser = UserUtil.getLoginUser();
        Activity activity = new Activity();
        BeanUtils.copyProperties(bo, activity);
        activity.setCreateBy(String.valueOf(loginUser.getUserId()));
        activity.setParticipants("0"); // 初始参与人数为0
        activityService.insertActivity(activity);
        return R.success(activity);
    }

    @PostMapping("/update")
    @ApiOperation("修改活动")
    public R updateActivityById(@RequestBody Activity activity) {
        LoginUser loginUser = UserUtil.getLoginUser();
        activity.setUpdateBy(String.valueOf(loginUser.getUserId()));
        activityService.updateActivityById(activity);
        return R.success();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除活动")
    public R delActivityById(@PathVariable String id) {
        activityService.delActivityById(id);
        return R.success();
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("获取活动详情")
    public R<ActivityListVo> getActivityDetail(@PathVariable Integer id) {
        ActivityListVo activity = activityService.selectActivityById(id);
        return R.success(activity);
    }
}

